#ifndef TCHART_H
#define TCHART_H

#include <QDialog>
#include <QGridLayout>
#include <qwt_plot_canvas.h>
#include <qwt_legend.h>
#include <qwt_plot_grid.h>
#include <TCurve.h>
#include <qwt_symbol.h>
#include <qwt_plot_zoomer.h>
#include "MainPlot.h"
#include <QTimer>
#include "common.h"
#include "WheelZoom.h"
#include <QMenuBar>
#include <QToolBar>
#include <QAction>
#include <QSplitter>
#include <QLabel>
#include <QListView>
#include <QSpinBox>
#include <QComboBox>
#include <QPushButton>
#include <QStringListModel>
#include <QCheckBox>
#include "TChartAxesScaler.h"
#include <QTabWidget>


class TChartItem
{
public:
    TChartItem()
    {
        Name = "";
        LineType = Qt::NoPen;
        Color = Qt::black;
        Width = 0;
        SplineSize = 0;
        DotSize = 0;
        DotType = QwtSymbol::NoSymbol;
        DotColor = Qt::black;
    }

    QString Name;
    Qt::PenStyle LineType;
    QColor Color;
    uint Width;
    uint SplineSize;
    uint DotSize;
    QwtSymbol::Style DotType;
    QColor DotColor;
};

//-------------------------------------

class TChart : public QFrame
{
    Q_OBJECT

    MainPlot    * mainPlot;
    QwtLegend   * leg;
    QwtPlotGrid * grid;
    QVector< TCurve* > curves;
    typedef QVector< TCurve* >::iterator QurveIter;
    WheelZoom * wzoom;
    TChartAxesScaler * axScaler;

    //QMenuBar * menuBar;
    QToolBar * toolPalette;

    QGridLayout    * glay;
    QSplitter      * splH;
    QListView      * listView;
    QWidget        * wgtControls ;
    QGridLayout    * glayControls;
    QPushButton    * btnClr;
    QLabel         * lblSplineDots;
    QSpinBox       * sbSplineDots;
    QLabel         * lblLineWidth;
    QSpinBox       * sbLineWidth;
    QLabel         * lblLineType;
    QComboBox      * cbLineType;
    QPushButton    * btnDotsColor;
    QLabel         * lblDotsSize;
    QSpinBox       * sbDotsSize;
    QLabel         * lblDotsType;
    QComboBox      * cbDotsType;
    QCheckBox      * chbUseSpline;
    QPushButton    * btnBndTop;
    QPushButton    * btnBndBottom;
    QPushButton    * btnBndLeft;
    QPushButton    * btnBndRight;
    QDoubleSpinBox * dsbBndTop;
    QDoubleSpinBox * dsbBndBottom;
    QDoubleSpinBox * dsbBndLeft;
    QDoubleSpinBox * dsbBndRight;
    QPushButton    * btnAdjustCrv;

    QComboBox * cbXScaleType;
    QComboBox * cbYScaleType;
    QLabel * lblXScaleType;
    QLabel * lblYScaleType;

    QTabWidget  * tabWgt;

    QStringListModel * mdl;
    
public:
    explicit TChart( QWidget *parent = 0 );
    virtual ~TChart();

    TCurve * CreateCurve( QVector< QPointF > & dots, QString name = "" );
    TCurve * CreateCurve( QVector< double > & x, QVector< double > & y, QString name = "" );
    
private:
    void createCurves();

signals:

private slots:
    void sltToolPaletteClick( QAction * a );
    void enableControls( QString & name );
    void execControl( QObject * obj );

private:
    TCurve * getCurveByName( QString & name );
    TCurve * getSelectedCurve();
    void renewCurvesList();
    void smoothing( QVector< QPointF > & dots );
    void addTableTab( QString name, QPolygonF & p );

private slots:
    void sltListviewClickItem( QModelIndex ind );
    void sltSetLineColor()     { execControl( btnClr ); }
    void sltSetWidth()         { execControl( sbLineWidth ); }
    void sltSetLineType()      { execControl( cbLineType ); }
    void sltSetSplineDotsNum() { execControl( sbSplineDots ); }
    void sltSetDotsColor()     { execControl( btnDotsColor ); }
    void sltSetDotsType()      { execControl( cbDotsType ); }
    void sltSetDotsSize()      { execControl( sbDotsSize ); }
    void sltUseSpline()        { execControl( chbUseSpline ); }
    void sltSetMaxX()          { execControl( btnBndRight ); }
    void sltSetMinX()          { execControl( btnBndLeft ); }
    void sltSetMaxY()          { execControl( btnBndTop ); }
    void sltSetMinY()          { execControl( btnBndBottom ); }
    void sltTopBndDsbRightClick()      { execControl( dsbBndTop ); }
    void sltBottomBndDsbRightClick()   { execControl( dsbBndBottom ); }
    void sltLeftBndDsbRightClick()     { execControl( dsbBndLeft ); }
    void sltRightBndDsbRightClick()    { execControl( dsbBndRight ); }
    void sltSetXScaleType()    { execControl( cbXScaleType ); }
    void sltSetYScaleType()    { execControl( cbYScaleType ); }
    void sltAdjustAxis2Crv()    { execControl( btnAdjustCrv ); }

};

#endif // TCHART_H
